package com.uchengwang.ui.cm_ui.cycle.fm;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.uchengwang.R;
import com.uchengwang.functions.http.config.BaseConfig;
import com.uchengwang.functions.http.listener.OnHttpResultListener;
import com.uchengwang.ui.UChengApplication;
import com.uchengwang.ui.base_ui.basefm.BaseFragment;
import com.uchengwang.ui.cm_ui.cycle.adapter.TalentPersonsAdapter;
import com.uchengwang.ui.cm_ui.cycle.adapter.TalentViewPagerAdapter;
import com.uchengwang.ui.cm_ui.cycle.bean.TalentPeopleBean;
import com.uchengwang.ui.cm_ui.cycle.http.CycleHttpUtils;
import com.uchengwang.ui.cm_ui.cycle.ui.ApplyTalentActivity;
import com.uchengwang.ui.utils.LoadingUtils;

import java.util.ArrayList;
import java.util.List;

import de.greenrobot.event.EventBus;

/**
 * Created by U城网 on 2018/6/14.
 * 街舞达人圈
 */

public class FragmentJiewu extends BaseFragment implements View.OnClickListener{

    public static long talent_jiewu = 12;
    private View view;
    private RecyclerView talent_person_rv;

    private TalentPersonsAdapter personsAdapter;

    private TabLayout tablayout;
    private ViewPager viewPage;
    private String[] s=new String[]{"最热","最新","本校"};
    private ArrayList<Fragment> list = new ArrayList<>();

    private TextView talent_apply, xiasha, school;
    private LinearLayout talent_person_more;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        setLayoutId(R.layout.fm_talent_music);
        view = super.onCreateView(inflater, container, savedInstanceState);

        EventBus.getDefault().register(this);
        initView();
        return view;
    }

    private void initView(){
        talent_apply = view.findViewById(R.id.talent_apply);
        talent_apply.setOnClickListener(this);

        xiasha = view.findViewById(R.id.talent_xiasha);
        xiasha.setOnClickListener(this);

        school = view.findViewById(R.id.talent_school);
        school.setOnClickListener(this);

        talent_person_rv = view.findViewById(R.id.talent_person_rv);

//        talent_person_more = view.findViewById(R.id.talent_person_more);
//        talent_person_more.setOnClickListener(this);

        tablayout = view.findViewById(R.id.tablayout);
        viewPage = view.findViewById(R.id.viewPage);

        initData();
    }

    private void initData(){
        personsAdapter = new TalentPersonsAdapter(getContext(), talent_jiewu);
        talent_person_rv.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.HORIZONTAL, false));
        talent_person_rv.setAdapter(personsAdapter);


        FragmentTalentInfos hot = new FragmentTalentInfos();
        Bundle hotB = new Bundle();
        hotB.putInt(BaseConfig.TYPE, 1);
        hotB.putLong(BaseConfig.CYCLE_ITEM_ID, talent_jiewu);
        hot.setArguments(hotB);

        FragmentTalentInfos newInfo = new FragmentTalentInfos();
        Bundle newInfoB = new Bundle();
        newInfoB.putInt(BaseConfig.TYPE, 2);
        newInfoB.putLong(BaseConfig.CYCLE_ITEM_ID, talent_jiewu);
        newInfo.setArguments(newInfoB);

        FragmentTalentInfos schoolInfo = new FragmentTalentInfos();
        Bundle schoolInfoB = new Bundle();
        schoolInfoB.putInt(BaseConfig.TYPE, 3);
        schoolInfoB.putLong(BaseConfig.CYCLE_ITEM_ID, talent_jiewu);
        schoolInfo.setArguments(schoolInfoB);

        list.clear();
        list.add(hot);
        list.add(newInfo);
        list.add(schoolInfo);
        Log.d("ucw", "list.size=="+list.size());

        TalentViewPagerAdapter adapter = new TalentViewPagerAdapter(getChildFragmentManager(), list, s);
        viewPage.setAdapter(adapter);
        tablayout.setupWithViewPager(viewPage);

        getTalentPeoples(0, false);

    }

    /**
     * 认证达人成功
     * */
    public void onEvent(String con) {
        if (!TextUtils.isEmpty(con) && con.equals(BaseConfig.APPLY_TALENT_SUCCESS)){
            getTalentPeoples(0, false);
        }
    }



    /**
     * 获取达人
     * 音乐圈id是21
     * */
    public void getTalentPeoples(long uniId, boolean isShow){
        if (isShow){
            LoadingUtils.getUtils().loading(getContext());
        }
        CycleHttpUtils.getUtils().getTalentPeoples(getActivity(), talent_jiewu, 1, 10, null, null, uniId,
                "talent_score", BaseConfig.DESC, new OnHttpResultListener<TalentPeopleBean>() {
                    @Override
                    public void onResult(int status, String msg, int total, List<TalentPeopleBean> list) {
                        LoadingUtils.getUtils().hide();
                        if (list != null){
                            talent_person_rv.setVisibility(View.VISIBLE);
                            personsAdapter.setData(list);
                        }
                    }
                });


    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        EventBus.getDefault().unregister(this);
    }

    @Override
    public void onClick(View v) {
        Intent intent = null;
        switch (v.getId()){
            case R.id.talent_apply:
                intent = new Intent(getContext(), ApplyTalentActivity.class);
                intent.putExtra(BaseConfig.CYCLE_ITEM_ID, talent_jiewu);
                intent.putExtra(BaseConfig.CYCLE_ITEM_NAME, "街舞");
                break;
            case R.id.talent_xiasha:
                xiasha.setTextColor(getResources().getColor(R.color.uni_title_bar));
                school.setTextColor(getResources().getColor(R.color.comment_color));
                getTalentPeoples(0, true);
                break;
            case R.id.talent_school:
                xiasha.setTextColor(getResources().getColor(R.color.comment_color));
                school.setTextColor(getResources().getColor(R.color.uni_title_bar));
                getTalentPeoples(UChengApplication.myUniId, true);
                break;

//            case R.id.talent_person_more:
//                intent = new Intent(getContext(), TalentListActivity.class);
//                intent.putExtra(BaseConfig.CYCLE_ITEM_ID, talent_jiewu);
//                break;
        }

        if (intent != null){
            startActivity(intent);
        }
    }
}
